package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.domain.CraftStepImg;
import com.uwlaser.service.CraftStepImgService;
import com.uwlaser.service.dto.CraftStepImgDto;
import com.uwlaser.service.dto.CraftStepImgQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.List;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "工步图片管理")
@RequestMapping("/api/craftStepImg")
public class CraftStepImgController {

    private final CraftStepImgService craftStepImgService;

    @GetMapping
    @Log("查询工步图片")
    @ApiOperation("查询工步图片")
    @PreAuthorize("@el.check('craftStepImg:list')")
    public ResponseEntity query(CraftStepImgQueryParam query, Pageable pageable){
        return new ResponseEntity<>(craftStepImgService.queryAll(query,pageable),HttpStatus.OK);
    }

    @GetMapping("/getByPid")
    @Log("根据工步ID查询工步图片")
    @ApiOperation("根据工步ID查询工步图片")
    @PreAuthorize("@el.check('craftStepImg:list','craftStepDetail:list')")
    public ResponseEntity getByPid(CraftStepImgQueryParam query){
        List<CraftStepImgDto> list = craftStepImgService.queryAll(query);
        if(list != null && list.size() > 0 ){
            return new ResponseEntity<>(list.get(0),HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping
    @Log("新增工步图片")
    @ApiOperation("新增工步图片")
    @PreAuthorize("@el.check('craftStepImg:add')")
    public ResponseEntity create(@Validated @RequestBody CraftStepImgDto resources){
        return new ResponseEntity<>(craftStepImgService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改工步图片")
    @ApiOperation("修改工步图片")
    @PreAuthorize("@el.check('craftStepImg:edit')")
    public ResponseEntity update(@Validated @RequestBody CraftStepImgDto resources){
        craftStepImgService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除工步图片")
    @ApiOperation("删除工步图片")
    @PreAuthorize("@el.check('craftStepImg:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        craftStepImgService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出工步图片")
    @ApiOperation("导出工步图片")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('craftStepImg:list')")
    public void download(HttpServletResponse response, CraftStepImgQueryParam query) throws IOException {
        craftStepImgService.download(craftStepImgService.queryAll(query), response);
    }*/

}
